package com.example.websocketserver;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;

/**
 * @author liulei
 * @version 1.0
 */
public class RuntimeTest {
    public static void main(String[] args) {
        try {
            // System信息，从jvm获取
            property();
            System.out.println("----------------------------------");
            // cpu信息
            cpu();
            System.out.println("----------------------------------");
            // 内存信息
            memory();
            System.out.println("----------------------------------");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public static void memory() throws SigarException {
        Sigar sigar = new Sigar();
        Mem mem = sigar.getMem();
        // 内存总量
        System.out.println("内存总量:" + mem.getTotal() / 1024L + "K av");
        // 当前内存使用量
        System.out.println("当前内存使用量:" + mem.getUsed() / 1024L + "K used");
        // 当前内存剩余量
        System.out.println("当前内存剩余量:" + mem.getFree() / 1024L + "K free");
        Swap swap = sigar.getSwap();
        // 交换区总量
        System.out.println("交换区总量:" + swap.getTotal() / 1024L + "K av");
        // 当前交换区使用量
        System.out.println("当前交换区使用量:" + swap.getUsed() / 1024L + "K used");
        // 当前交换区剩余量
        System.out.println("当前交换区剩余量:" + swap.getFree() / 1024L + "K free");
    }

    public static void property() throws UnknownHostException {
        Runtime r = Runtime.getRuntime();
        InetAddress addr;
        addr = InetAddress.getLocalHost();
        String ip = addr.getHostAddress();
        Map<String, String> map = System.getenv();
        // 获取计算机域名
        String userDomain = map.get("USERDOMAIN");
        // System.out.println("计算机域名:" + userDomain + ",本地ip地址:" + ip + ",JVM可以使用的处理器个数:" + r.availableProcessors());
        System.out.println("JVM可以使用的总内存:" + r.totalMemory() / 1024 / 1024 + "Mb,剩余内存:" + r.freeMemory() / 1024 / 1024 + "Mb");
    }

    public static void cpu() throws SigarException {
        Sigar sigar = new Sigar();
        CpuInfo infos[] = sigar.getCpuInfoList();
        CpuPerc cpuList[];
        cpuList = sigar.getCpuPercList();
        // 不管是单块CPU还是多CPU都适用
        for (int i = 0; i < infos.length; i++) {
            CpuInfo info = infos[i];
            System.out.println("第" + (i + 1) + "块CPU信息");
            System.out.println("CPU的总量MHz:" + info.getMhz());
            System.out.println("CPU缓存数量:" + info.getCacheSize());
            printCpuPerc(cpuList[i]);
        }
    }

    public static void printCpuPerc(CpuPerc cpu) {
        System.out.println("CPU用户使用率:" + CpuPerc.format(cpu.getUser()));
        System.out.println("CPU系统使用率:" + CpuPerc.format(cpu.getSys()));
        System.out.println("CPU当前等待率:" + CpuPerc.format(cpu.getWait()));
        System.out.println("CPU当前空闲率:" + CpuPerc.format(cpu.getIdle()));
        System.out.println("CPU总的使用率:" + CpuPerc.format(cpu.getCombined()));
    }

}
